﻿Imports System.Windows.Forms

Public Class SettingForm
    Dim TokenObject As New Object
    Dim TwiSender As TwitterOAuth = TwitterSender.TwiSender
    Private Sub ApplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyButton.Click
        With My.Settings
            .UseOAuth = UseOAuthButton.Checked
            .Username = UsernameBox.Text
            .Password = PasswordBox.Text
            .API = APIURLBox.Text
            .UseProxy = SetProxyBox.Checked
            .NoProxy = NoProxyBox.Checked
            .ProxyAddress = ProxyURLBox.Text
            Integer.TryParse(ProxyPortBox.Text, .ProxyPort)
            .ProxyUsername = ProxyUsernameBox.Text
            .ProxyPassword = ProxyPasswordBox.Text
            .UseAPIForOAuth = UseOAuthAPIBox.Checked
        End With
        If sender Is TokenObject Then Return
        Me.Close()
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBtn.Click
        Me.Close()
    End Sub

    Private Sub SettingForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        With My.Settings
            UseOAuthButton.Checked = .UseOAuth
            UseBasicButton.Checked = Not .UseOAuth
            UseOAuthAPIBox.Checked = .UseAPIForOAuth
            UsernameBox.Text = .Username
            PasswordBox.Text = .Password
            APIURLBox.Text = .API
            If .UseProxy Then
                SetProxyBox.Checked = True
            ElseIf .NoProxy Then
                NoProxyBox.Checked = True
            Else
                IEProxyBox.Checked = True
            End If
            ProxyURLBox.Text = .ProxyAddress
            ProxyPortBox.Text = .ProxyPort.ToString
            ProxyUsernameBox.Text = .ProxyUsername
            ProxyPasswordBox.Text = .ProxyPassword
        End With
        If HaveToken() Then PINBox.Text = "OAuth Token存在" Else PINBox.Text = "需要进行OAuth认证"
    End Sub

    Dim token = String.Empty
    Dim verifying = False

    Private Sub DoOAuthButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoOAuthButton.Click
        ApplyButton_Click(TokenObject, Nothing)
        If verifying Then
            Try
                Dim tokensecret = My.Settings.TokenSecret
                If TwiSender.GetAccessToken(token, tokensecret, PINBox.Text) Then
                    TwiSender.AddToken(token, tokensecret)
                    My.Settings.Token = token
                    My.Settings.TokenSecret = tokensecret
                Else
                    MessageBox.Show("验证失败，请检查PIN，或者再试一次")
                End If
            Catch ex As Exception
                MessageBox.Show("验证时发生错误", "出错", MessageBoxButtons.OK)
            Finally
                DoOAuthButton.Text = "进行OAuth认证"
                verifying = False
            End Try
        Else
            Try
                token = String.Empty
                For i = 1 To 5
                    TwiSender.GetRequestToken(token)
                    If Not String.IsNullOrEmpty(token) Then
                        Exit For
                    End If
                Next
                Process.Start(TwiSender.RedirectToAuthorize(token))
                My.Settings.Token = token
                DoOAuthButton.Text = "↓填写PIN后按这里↓"
                verifying = True
                PINBox.Text = ""
                PINBox.Select()
            Catch ex As Exception
                MessageBox.Show("无法连接到Twitter！", "无法连接", MessageBoxButtons.OK)
            End Try
        End If
    End Sub
End Class